//
//  TJTaskManagerDetailCell.m
//  BaoDian
//
//  Created by mac on 2024/8/3.
//

#import "TJTaskManagerDetailCell.h"
#import "TJUserPublishDataView.h"
#import "TJButton.h"
#import "TJVideoCoverDownloader.h"

@interface TJTaskManagerDetailCell()

@property (nonatomic, weak) UILabel *addressLabel;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) UILabel *stateLabel;
@property (nonatomic, weak) TJTaskDouYinDataView *dataView;
@property (nonatomic, weak) UILabel *merchantNameLabel;
@property (nonatomic, weak) UIImageView *coverImageView;


@end

@implementation TJTaskManagerDetailCell

- (void)settingSubView {
    [super settingSubView];
    
    self.backgroundColor = UIColor.tj_1E1F23Color;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIImageView *coverImageView = [UIImageView addToView:self.contentView];
    coverImageView.addCornerRadius(8);
    coverImageView.backgroundColor = UIColorMake(0xCECECD);
    [coverImageView addTapGesturesWithTarget:self action:@selector(tapCoverImageView:)];
    [coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(16);
        make.left.offset(16);
        make.width.offset(60);
        make.height.offset(80);
    }];
    
    UIImageView *playerIconView = [UIImageView addToView:coverImageView];
    playerIconView.image = [UIImage imageNamed:@"videoPlayer_icon"];
    [playerIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
        make.width.height.offset(24);
    }];
    
    UILabel *addressLabel = [UILabel lableWithSuperView:self.contentView font:UIFontBoldMake(14) color:UIColor.whiteColor title:nil textAlignment:NSTextAlignmentLeft];
    addressLabel.addCornerRadius(4);
    addressLabel.numberOfLines = 2;
    [addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(coverImageView.mas_right).offset(10);
        make.top.equalTo(coverImageView);
        make.right.mas_lessThanOrEqualTo(-16);
    }];
    
    UIImageView *avatarImageView = [UIImageView addToView:self.contentView];
    avatarImageView.addCornerRadius(10);
    [avatarImageView tj_setImageWithURL:TJUserInfo.avatar.URL placeholderImage:[UIImage imageNamed:defaultAvatarName]];
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(coverImageView);
        make.left.equalTo(coverImageView.mas_right).offset(10);
        make.width.height.offset(20);
    }];
    
    UILabel *nameLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.whiteColor title:nil textAlignment:NSTextAlignmentLeft];
    nameLabel.numberOfLines = 2;
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarImageView.mas_right).offset(4);
        make.centerY.equalTo(avatarImageView);
        make.right.mas_lessThanOrEqualTo(-76);
    }];
    
    UILabel *stateLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColorMake(0x59DD44) title:@"已打卡" textAlignment:NSTextAlignmentLeft];
    [stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(16);
        make.centerY.equalTo(nameLabel);
    }];
    
    TJTaskDouYinDataView *dataView = [TJTaskDouYinDataView addToView:self.contentView];
    dataView.backgroundColor = UIColorMake(0x2A2B31);
    dataView.addCornerRadius(8);
    dataView.playerCountItem.textLabel.text = @"_";
    dataView.likeCountItem.textLabel.text = @"_";
    dataView.commentCountItem.textLabel.text = @"_";
    [dataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(16);
        make.height.offset(52);
        make.top.equalTo(coverImageView.mas_bottom).offset(16);
    }];
    
    UIView *bottomView = [UIView addToView:self.contentView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(0);
        make.bottom.offset(0);
        make.top.equalTo(dataView.mas_bottom);
    }];
    
    UILabel *douYinNameLabel = [UILabel lableWithSuperView:bottomView font:UIFontMake(12) color:UIColor.tj_8D9298Color title:@"挂载商家" textAlignment:NSTextAlignmentLeft];
    [douYinNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(dataView);
        make.centerY.offset(0);
    }];
 
    UILabel *merchantNameLabel = [UILabel lableWithSuperView:bottomView font:UIFontMake(12) color:UIColor.whiteColor title:@"-/商家名称烤全羊" textAlignment:NSTextAlignmentLeft];
    [merchantNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(dataView);
        make.centerY.offset(0);
    }];
    
    _addressLabel = addressLabel;
    _nameLabel = nameLabel;
    _avatarImageView = avatarImageView;
    _stateLabel = stateLabel;
    _dataView = dataView;
    _merchantNameLabel = merchantNameLabel;
    _coverImageView = coverImageView;
    _stateLabel = stateLabel;
}

- (void)setModel:(TJTaskModel *)model {
    _model = model;
    
    _addressLabel.text = model.text;
    [_avatarImageView tj_setImageWithURL:model.pic.URL placeholderImage:[UIImage imageNamed:defaultAvatarName]];
    _nameLabel.text = model.nickName;
    _dataView.playerCountItem.textLabel.text = model.playCount;
    _dataView.likeCountItem.textLabel.text = model.diggCount;
    _dataView.commentCountItem.textLabel.text = model.commentCount;
    
    [[TJVideoCoverDownloader shareDownLoader] downloadWithUrl:model.videoUrl completion:^(UIImage * _Nullable thumbnail) {
        self.coverImageView.image = thumbnail;
    }];
    
    if ([model.packType intValue] == 0) {
        _merchantNameLabel.text = @"-/无挂载";
    } else if ([model.packType intValue] == 1) {
        _merchantNameLabel.text = @"-/";
    } else if ([model.packType intValue] == 2) {
        _merchantNameLabel.text = model.microAppTitle;
    } else {
        _merchantNameLabel.text = @"-/";
    }
    
    if (model.status.intValue == 1) {
        _stateLabel.textColor = UIColorMake(0xFFAC3F);
        _stateLabel.text = @"待发布";
    } else if (model.status.intValue == 2) {
        _stateLabel.textColor = UIColorMake(0x59DD44);
        _stateLabel.text = @"已发布";
    } else if (model.status.intValue == 4) {
        _stateLabel.text = nil;
    }
}

- (void)tapCoverImageView:(UITapGestureRecognizer *)tap {
    UIImageView *imageView = (UIImageView *)tap.view;
    if (_tapCoverImageViewBlock) {
        _tapCoverImageViewBlock(imageView.image);
    }
}

@end
